2012-04-05 20 views
5

Tôi tạo ra một hàm để tách một hình ảnh thành nhiều hình ảnh, nhưng khi tôi đi lấy CGImage của UIImage, các CGImage trả về NULLCGImage của UIImage trả về NULL

NSArray* splitImage(UIImage* image,NSUInteger pieces) { 

NSLog(@"width: %f, %zu",image.size.width,CGImageGetWidth(image.CGImage)); 
NSLog(@"%@",image.CGImage); 
returns NULL 
NSMutableArray* tempArray = [[NSMutableArray alloc]initWithCapacity:pieces]; 

CGFloat piecesSize = image.size.height/pieces; 

for (NSUInteger i = 0; i < pieces; i++) { 

    // take in account retina displays 
    CGRect subFrame = CGRectMake(0,i * piecesSize * image.scale ,image.size.width * image.scale,piecesSize * image.scale); 

    CGImageRef newImage = CGImageCreateWithImageInRect(image.CGImage,subFrame); 

    UIImage* finalImage =[UIImage imageWithCGImage:newImage]; 

    CGImageRelease(newImage); 

    [tempArray addObject:finalImage]; 

} 

NSArray* finalArray = [NSArray arrayWithArray:tempArray]; 

[tempArray release]; 

return finalArray; 



} 
+0

séc tempArray là null hay không ở cuối – Hector

+1

CGImage không phải là một đối tượng, nó là một cấu trúc C, vì vậy bạn không nên sử dụng một NSLog với% @. – lnafziger

+0

Vấn đề là tôi tạo UIImage bằng IOSurface: D – Otium

Trả lời

4

Thuộc tính CGImage sẽ trở lại con số không nếu UIImage đã được tạo ra từ hình ảnh khác như một IOSurface hoặc CIImage. Để giải quyết vấn đề này trong trường hợp cụ thể này, tôi có thể tạo một CGImage từ một IOSurface bằng cách sử dụng hàm c sau đó chuyển đổi nó thành một UIImage.

UICreateCGImageFromIOSurface(IOSurfaceRef surface); 
2

gì bạn đã làm bây giờ chỉ là tạo mảnh với 0.f chiều rộng, bạn nên sử dụng hai for s để xác định width & height cho các phần của bạn. Mẫu mã như thế này (chưa được thử nghiệm nhưng phải hoạt động):

for (int height = 0; height < image.size.height; height += piecesSize) { 
    for (int width = 0; width < image.size.width; width += piecesSize) { 
    CGRect subFrame = CGRectMake(width, height, piecesSize, piecesSize); 

    CGImageRef newImage = CGImageCreateWithImageInRect(image.CGImage, subFrame); 
    UIImage * finalImage = [UIImage imageWithCGImage:newImage]; 
    CGImageRelease(newImage); 

    [tempArray addObject:finalImage]; 
    } 
} 
+1

+1 Tôi có (thử nghiệm) mã mà thực hiện điều này, và nó phù hợp với đề xuất của bạn. Mỏ được đóng gói như một phương pháp thể loại trên UIImage gọi là subimageInRect: (CGRect) rect. Tôi nghĩ đó là một cách dễ chịu hơn. – danh

1

Điều này xảy ra trong một số trường hợp khi chúng tôi cố gắng cắt hình ảnh. Tôi tìm thấy giải pháp như thế này thử này có thể là điều này có thể giúp bạn: -

NSData *imageData = UIImageJPEGRepresentation(yourImage, 0.9); 
newImage = [UIImage imageWithData:imageData]; 
2

Tôi đã tạo UIImage từ CGImage.

CIImage *ciImage = image.CIImage; 
CIContext *context = [CIContext contextWithOptions:nil]; 
CGImageRef ref = [context createCGImage:ciImage fromRect:ciImage.extent]; 
UIImage *newImage = [UIImage imageWithCGImage:ref]; 

Và bây giờ newImage.CGImage không nil