2010-02-10 15 views
13

के साथ विफल रहता है मैं फसल के लिए निम्न कोड का उपयोग कर रहा हूं और एक बड़ा यूआईएममेज बड़ा से बाहर कर रहा हूं। मैंने इस मुद्दे को CGImageCreateWithImageInRect() के साथ होने के लिए अलग कर दिया है, जो कि कुछ CGImage संपत्ति को जिस तरह से चाहता है उसे सेट नहीं करता है। :-) समस्या यह है कि UIImagePNGRepresentation() को कार्य करने के लिए एक कॉल एक शून्य लौटने में विफल रहता है।CGImageRef से बनाया गया UIImage UIImagePNGRresentation

CGImageRef origRef = [stillView.image CGImage]; 
CGImageRef cgCrop = CGImageCreateWithImageInRect(origRef, theRect); 
UIImage *imgCrop = [UIImage imageWithCGImage:cgCrop]; 

...

NSData *data = UIImagePNGRepresentation (imgCrop); 

- libpng त्रुटि: कोई IDATs फ़ाइल

में लिखा किसी भी विचार क्या हो सकता है गलत या UIImage के बाहर एक रेक्ट फसल के लिए विकल्प? बहुत धन्यवाद!

+0

मुझे "libpng त्रुटि: फ़ाइल में कोई आईडीएटी लिखा नहीं गया" जब मैंने लिंक समय अनुकूलन सक्षम किया। क्या आपने यह विकल्प सक्षम किया था? इसे अक्षम करने के लिए इसे मेरे लिए तय किया गया। – rasmus

उत्तर

1

एक पीएनजी में विभिन्न भाग मौजूद हैं, जिनमें पैलेट जानकारी, कुछ वास्तविक छवि डेटा और कुछ अन्य जानकारी है, यह एक बहुत ही रोचक मानक है। आईडीएटी खंड थोड़ा सा है जिसमें वास्तव में छवि डेटा होता है। यदि कोई फ़ाइल में "आईडीएटी लिखा नहीं गया है" तो libpng में इनपुट डेटा से पीएनजी बनाने में कुछ समस्या आई है।

मुझे नहीं पता कि आपका अभी भी क्या दृश्य है। छवि है, लेकिन जब आप अपना कोड एक CGImageRef पास करते हैं तो निश्चित रूप से मान्य होता है? Rect में वास्तविक मान क्या हैं? यदि आपका theRect छवि की सीमाओं से परे है तो आप जिस सीजीक्रॉप को यूआईएममेज बनाने के लिए उपयोग करने की कोशिश कर रहे हैं वह आसानी से शून्य हो सकता है - या शून्य नहीं, लेकिन चौड़ाई और ऊंचाई 0 के साथ कोई छवि या छवि नहीं है, जो libpng को काम करने के लिए कुछ भी नहीं दे रहा है साथ में।

+0

उत्तर के लिए धन्यवाद। अभी भी दृश्य UIImageView है। मैंने सीजीक्रॉप और आईएमजी क्रॉप की संपत्ति और फ़ंक्शन रिटर्न वैल्यू को जीडीबी पी/पीओ कमांड के साथ देखा और दोनों में सही आयाम प्रतीत होता है, सीजीक्रॉप इत्यादि के लिए आईजीजी क्रॉप सीजीआईमेज संदर्भ बिंदु। काम से वापस जब एक और विस्तृत देखो होगा। –

1

यह soultion आप काम करना चाहिए कोशिश कर रहे हैं लगता है, लेकिन मैं इस का उपयोग करने की सलाह देते हैं:

CGImageRef image = [stillView.image CGImage]; 
CGRect cropZone; 

size_t cWitdh = cropZone.size.width; 
size_t cHeight = cropZone.size.height; 
size_t bitsPerComponent = CGImageGetBitsPerComponent(image); 
size_t bytesPerRow = CGImageGetBytesPerRow(image)/CGImageGetWidth(image) * cWidth; 

//Now we build a Context with those dimensions. 
CGContextRef context = CGBitmapContextCreate(nil, cWitdh, cHeight, bitsPerComponent, bytesPerRow, CGColorSpaceCreateDeviceRGB(), CGImageGetBitmapInfo(image)); 

CGContextDrawImage(context, cropZone, image); 

CGImageRef result = CGBitmapContextCreateImage(context); 
UIImage * cropUIImage = [[UIImage alloc] initWithCGImage:tmp]; 

CGContextRelease(context); 
CGImageRelease(mergeResult); 
NSData * imgData = UIImagePNGRepresentation (cropUIImage); 

आशा है कि यह मदद करता है।

+0

यदि आपकी मूल छवि "बहुत बड़ी" है तो मुझे आश्चर्य है कि एक विकल्प मूल छवि का उपयोग करेगा जहां आपको उप-आवश्यकता की आवश्यकता होगी, लेकिन ड्राइंग से पहले एक क्लिपिंग मुखौटा सेट करें। इससे आपको मूल छवि से "कॉपी" बनाने से बचने की अनुमति मिल जाएगी। इस दृष्टिकोण में इसकी अपनी समस्या हो सकती है, इसलिए आपको 2 विधियों को "तुलना और विपरीत" करना होगा। – nielsbot

+0

ठीक है - जो मैं प्रस्तावित कर रहा हूं वह मूल रूप से प्रस्तावित किया गया है। मुझे लगता है कि सिकारियो की विधि संभावित रूप से बहुत अधिक स्मृति का उपयोग कर सकती है। – nielsbot

3

मुझे एक ही समस्या थी, लेकिन केवल आईओएस 3.2 पर संगतता का परीक्षण करते समय। 4.2 पर यह ठीक काम करता है।

अंत में मुझे यह http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/ मिला जो दोनों पर काम करता है, हालांकि थोड़ा और वर्बोज़!

मैं UIImage पर एक वर्ग में इस परिवर्तित:

UIImage + Crop.h

@interface UIImage (Crop) 
- (UIImage*) imageByCroppingToRect:(CGRect)rect; 
@end 

UIImage + Crop.m

@implementation UIImage (Crop) 

- (UIImage*) imageByCroppingToRect:(CGRect)rect 
{ 
    //create a context to do our clipping in 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    //create a rect with the size we want to crop the image to 
    //the X and Y here are zero so we start at the beginning of our 
    //newly created context 
    CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height); 
    CGContextClipToRect(currentContext, clippedRect); 

    //create a rect equivalent to the full size of the image 
    //offset the rect by the X and Y we want to start the crop 
    //from in order to cut off anything before them 
    CGRect drawRect = CGRectMake(rect.origin.x * -1, 
           rect.origin.y * -1, 
           self.size.width, 
           self.size.height); 

    //draw the image to our clipped context using our offset rect 
    CGContextTranslateCTM(currentContext, 0.0, rect.size.height); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    CGContextDrawImage(currentContext, drawRect, self.CGImage); 

    //pull the image from our cropped context 
    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext(); 

    //pop the context to get back to the default 
    UIGraphicsEndImageContext(); 

    //Note: this is autoreleased 
    return cropped; 
} 


@end 
+0

यह तंत्र फसल के अलावा किसी अन्य संदर्भ में उपयोगी हो सकता है। मेरे पास एक यूआईएममेज था, संभवतः असामान्य रंग स्थान में परिभाषित होने के कारण, UIImagePNGRepresentation द्वारा स्वीकार नहीं किया गया था। मैंने UIImagePNGRepresentation को स्वीकार्य रूप में UIImage को पुन: उत्पन्न करने के लिए उपर्युक्त कोड का उपयोग किया। –

0

हे मैं में तर्क के इस प्रकार का उपयोग अपने ऐप, उम्मीद है कि यह आपकी मदद करेगा ...

UIImage *croppedImage = [self imageByCropping:yourImageView.image toRect:heredefineyourRect]; 

    CGSize size = CGSizeMake(croppedImage.size.height, croppedImage.size.width); 
    UIGraphicsBeginImageContext(size); 

    CGPoint pointImg1 = CGPointMake(0,0); 
    [croppedImage drawAtPoint:pointImg1 ]; 

    [[UIImage imageNamed:yourImagenameDefine] drawInRect:CGRectMake(0,532, 150,80) ];//here define your Reactangle 

    UIImage* result = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    croppedImage = result; 
    yourCropImageView.image=croppedImage; 
    [yourCropImageView.image retain]; 

उम्मीद है कि यह आपकी मदद करेगा .... :)