2011-07-18 98 views
1

我需要从相机捕捉照片并将其保存在文件夹中。如果我尝试从文件夹中显示多个图像。它不会加载,抛出内存警告并关闭应用程序。如果我从照片库中选择图像,此过程正常工作。我错过了哪里请指教。如何减少iPhone中的摄像头图像文件大小?

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    [self dismissModalViewControllerAnimated:YES]; 
    isPrivacyPhoto = NO; 
    UIImage *pickedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    NSData *jpegData = UIImageJPEGRepresentation(pickedImage,1.0); 
     [jpegData writeToFile:fileDirectory atomically:NO]; 


} 


thumnail = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@%@",photo.Path,photo.PhotoName]]; 

thumnail=[appDelegate thumnail scaledToSize:CGSizeMake(75.0, 104.0)]; 



-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize 
{ 

    UIGraphicsBeginImageContextWithOptions(newSize, YES, [UIScreen mainScreen].scale); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 

} 

我重视我的源代码,供大家参考:

for (int Count = 0; Count < [listData count] ; Count ++) 
{ 
    Photo *photo = [listData objectAtIndex: Count]; 

    if([FileUtils fileExistsAtPath:photo.Path fileName:photo.PhotoName]) 
    { 

     PhotoView *photoView = [[PhotoView alloc] initWithFrame: CGRectMake(ThumbnailSizeWidth * (PhotoViewCount % THUMBNAIL_COLS) + PADDING * (PhotoViewCount % THUMBNAIL_COLS) + PADDING, 
                      ThumbnailSizeHeight * (PhotoViewCount/THUMBNAIL_COLS) + PADDING * (PhotoViewCount/THUMBNAIL_COLS) + PADDING + PADDING_TOP, 
                      ThumbnailSizeWidth, 
                      ThumbnailSizeHeight)]; 
     [photoView setDelegate:self]; 
     [photoView setPhoto:photo]; 
     [photoView setTagIndexID:OrginalCounter]; 
     //NSLog(@"TagIndexID:%d",Count); 
     PhotoViewCount ++ ; 


     if(photo.isPrivacy) 
     { 
      UIImage *tImage = [UIImage imageNamed:@"locked.png"]; 
      [photoView setPhotoViewImage:tImage]; 
     } 
     else 
     { 
      [photoView setTag:OrginalCounter]; 

      NSData *imgData = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@%@",photo.Path,photo.PhotoName]]; 
      UIImage *thumnail = [UIImage imageWithData:imgData]; 


      //UIImage *thumnail = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@%@",photo.Path,photo.PhotoName]]; 
      //UIImage *thumnail = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@%@",photo.Path,photo.PhotoName]]; 

      MyPhoto *photo = [[MyPhoto alloc] initWithImage:thumnail]; 
      [photos addObject:photo]; 
      [photo release]; 
      //[thumnail release]; 


      OrginalCounter++; 

      [photoView performSelectorInBackground:@selector(setPhotoViewImage:) withObject:thumnail]; 

     } 

     [scrollViewer addSubview:photoView]; 
     [photoView release]; 


    } 
} 




-(void) setPhotoViewImage:(UIImage*)image 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 


    PrivacyPixAppDelegate *appDelegate = [PrivacyPixAppDelegate appDelegate]; 

    image=[appDelegate imageWithImage:image scaledToSize:CGSizeMake(75.0, 104.0)]; 
    //image = [image scaleAndCropToSize:CGSizeMake(69.0, 104.0) onlyIfNeeded:YES]; 
    //image = [image scaleAndCropToSize:CGSizeMake(75, 100)]; 

    //image=[image ScaleImageToRect:image displaySize:CGSizeMake(40,40)]; 


    if(!btnPhoto) 
     btnPhoto = [[UIImageView alloc] init]; 
     [btnPhoto setFrame:CGRectMake(2, 2, 75, 75)]; 
    //[btnPhoto setContentMode:UIViewContentModeTop]; 
    btnPhoto.image = image; 
    [self addSubview:btnPhoto]; 
    //[btnPhoto release]; 


    if(!txtPhotoName) 
     txtPhotoName = [[UITextField alloc] init]; 

     [txtPhotoName setDelegate:self]; 
    [txtPhotoName setFrame:CGRectMake(2, btnPhoto.frame.size.height + 2, self.frame.size.width, 20)]; 
    txtPhotoName.font = [UIFont boldSystemFontOfSize:12.0]; 
    txtPhotoName.backgroundColor = [UIColor whiteColor]; 
    txtPhotoName.textAlignment = UITextAlignmentCenter; 
    txtPhotoName.borderStyle = UITextBorderStyleLine; 
    txtPhotoName.text = photo.PhotoCaption; 
    txtPhotoName.returnKeyType = UIReturnKeyDone; 
    txtPhotoName.hidden = YES; 
    [self addSubview:txtPhotoName]; 


    if(!lblPhotoName) 
     lblPhotoName = [[UILabel alloc] init]; 

     [lblPhotoName setFrame:CGRectMake(0, btnPhoto.frame.size.height, self.frame.size.width, 25)]; 
    lblPhotoName.backgroundColor = [UIColor clearColor]; 
    lblPhotoName.font = [UIFont systemFontOfSize:9.0]; 
    lblPhotoName.userInteractionEnabled = NO; 
    lblPhotoName.text = photo.PhotoCaption; 
    lblPhotoName.numberOfLines = 2; 
    lblPhotoName.hidden = NO; 
    lblPhotoName.textAlignment = UITextAlignmentCenter; 
    [self addSubview:lblPhotoName]; 



    //[lblPhotoName release]; 

    [pool release]; 
} 


-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize 
{ 
    // Create a bitmap context. 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 

} 

这是我的代码,我越来越内存华林和应用程序崩溃。我必须改变它。 注意:应用程序崩溃只发生在相机图像上。

:收到内存警告。等级= 2

+0

你想裁剪图像? – Maulik

回答

1

从这篇文章摘自:How do I reduce Image quality/size in iPhone objective-c?

您可以创建一个图形上下文,绘制图像成在所需的规模,并使用返回的图像。例如:

UIGraphicsBeginImageContext(CGSizeMake(480,320)); 
CGContextRef   context = UIGraphicsGetCurrentContext(); 
[image drawInRect: CGRectMake(0, 0, 480, 320)]; 
UIImage  *smallImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

只需修改上面的大小以减少图像为任何你喜欢的。

+0

请检阅我的完整资源并引导我 – Gnanavadivelu

+3

当然。我的小时费率是90美元。 – PengOne